setwd("/Users/msbell/Documents/My Documents/Minnesota/Work/Research/Security Dilemma/Publication version/Replication/")
library(gplots)

China<-read.csv("China2020_for_analysis.csv")
US<-read.csv("US2020_for_analysis.csv")
China23<-read.csv("Ch_2023_for_analysis.csv")
US23<-read.csv("US_2023_for_analysis.csv")

USgCh<-US23[US23$USgivenCh==1,]
USgUS<-US23[US23$USgivenUS==1,]

ChgCh<-China23[China23$ChgivenCh==1,]
ChgUS<-China23[China23$ChgivenUS==1,]

#Treatment effects in China 2020
a<-t.test(China[China$control==1,]$defspend,China[China$offdef==1,]$defspend, var.equal=F)
b<-t.test(China[China$control==1,]$defspend,China[China$secdim==1,]$defspend, var.equal=F)
c<-t.test(China[China$control==1,]$defspend,China[China$nukes==1,]$defspend, var.equal=F)
d<-t.test(China[China$control==1,]$defspend,China[China$interd==1,]$defspend, var.equal=F)

e<-t.test(China[China$control==1,]$USisdef,China[China$offdef==1,]$USisdef, var.equal=F)
f<-t.test(China[China$control==1,]$USisdef,China[China$secdim==1,]$USisdef, var.equal=F)
g<-t.test(China[China$control==1,]$USisdef,China[China$nukes==1,]$USisdef, var.equal=F)
h<-t.test(China[China$control==1,]$USisdef,China[China$interd==1,]$USisdef, var.equal=F)

i<-t.test(China[China$control==1,]$USisthreat,China[China$offdef==1,]$USisthreat, var.equal=F)
j<-t.test(China[China$control==1,]$USisthreat,China[China$secdim==1,]$USisthreat, var.equal=F)
k<-t.test(China[China$control==1,]$USisthreat,China[China$nukes==1,]$USisthreat, var.equal=F)
l<-t.test(China[China$control==1,]$USisthreat,China[China$interd==1,]$USisthreat, var.equal=F)


#Treatment effects in US 2020
m<-t.test(US[US$control==1,]$defspend,US[US$offdef==1,]$defspend, var.equal=F)
n<-t.test(US[US$control==1,]$defspend,US[US$secdim==1,]$defspend, var.equal=F)
o<-t.test(US[US$control==1,]$defspend,US[US$nukes==1,]$defspend, var.equal=F)
p<-t.test(US[US$control==1,]$defspend,US[US$interd==1,]$defspend, var.equal=F)

q<-t.test(US[US$control==1,]$Chisdef,US[US$offdef==1,]$Chisdef, var.equal=F)
r<-t.test(US[US$control==1,]$Chisdef,US[US$secdim==1,]$Chisdef, var.equal=F)
s<-t.test(US[US$control==1,]$Chisdef,US[US$nukes==1,]$Chisdef, var.equal=F)
t<-t.test(US[US$control==1,]$Chisdef,US[US$interd==1,]$Chisdef, var.equal=F)

u<-t.test(US[US$control==1,]$Chisthreat,US[US$offdef==1,]$Chisthreat, var.equal=F)
v<-t.test(US[US$control==1,]$Chisthreat,US[US$secdim==1,]$Chisthreat, var.equal=F)
w<-t.test(US[US$control==1,]$Chisthreat,US[US$nukes==1,]$Chisthreat, var.equal=F)
x<-t.test(US[US$control==1,]$Chisthreat,US[US$interd==1,]$Chisthreat, var.equal=F)

#Treatment effects in China 2023
aa<-t.test(ChgUS[ChgUS$control==1,]$defspend,ChgUS[ChgUS$offdef==1,]$defspend, var.equal=F)
bb<-t.test(ChgUS[ChgUS$control==1,]$defspend,ChgUS[ChgUS$secdim==1,]$defspend, var.equal=F)
cc<-t.test(ChgUS[ChgUS$control==1,]$defspend,ChgUS[ChgUS$nukes==1,]$defspend, var.equal=F)
dd<-t.test(ChgUS[ChgUS$control==1,]$defspend,ChgUS[ChgUS$interd==1,]$defspend, var.equal=F)

ee<-t.test(ChgUS[ChgUS$control==1,]$USdef,ChgUS[ChgUS$offdef==1,]$USdef, var.equal=F)
ff<-t.test(ChgUS[ChgUS$control==1,]$USdef,ChgUS[ChgUS$secdim==1,]$USdef, var.equal=F)
gg<-t.test(ChgUS[ChgUS$control==1,]$USdef,ChgUS[ChgUS$nukes==1,]$USdef, var.equal=F)
hh<-t.test(ChgUS[ChgUS$control==1,]$USdef,ChgUS[ChgUS$interd==1,]$USdef, var.equal=F)

ii<-t.test(ChgUS[ChgUS$control==1,]$USisthreat,ChgUS[ChgUS$offdef==1,]$USisthreat, var.equal=F)
jj<-t.test(ChgUS[ChgUS$control==1,]$USisthreat,ChgUS[ChgUS$secdim==1,]$USisthreat, var.equal=F)
kk<-t.test(ChgUS[ChgUS$control==1,]$USisthreat,ChgUS[ChgUS$nukes==1,]$USisthreat, var.equal=F)
ll<-t.test(ChgUS[ChgUS$control==1,]$USisthreat,ChgUS[ChgUS$interd==1,]$USisthreat, var.equal=F)

#Treatment effects in US 2023
mm<-t.test(USgCh[USgCh$control==1,]$defspend,USgCh[USgCh$offdef==1,]$defspend, var.equal=F)
nn<-t.test(USgCh[USgCh$control==1,]$defspend,USgCh[USgCh$secdim==1,]$defspend, var.equal=F)
oo<-t.test(USgCh[USgCh$control==1,]$defspend,USgCh[USgCh$nukes==1,]$defspend, var.equal=F)
pp<-t.test(USgCh[USgCh$control==1,]$defspend,USgCh[USgCh$interd==1,]$defspend, var.equal=F)

qq<-t.test(USgCh[USgCh$control==1,]$Chdef,USgCh[USgCh$offdef==1,]$Chdef, var.equal=F)
rr<-t.test(USgCh[USgCh$control==1,]$Chdef,USgCh[USgCh$secdim==1,]$Chdef, var.equal=F)
ss<-t.test(USgCh[USgCh$control==1,]$Chdef,USgCh[USgCh$nukes==1,]$Chdef, var.equal=F)
tt<-t.test(USgCh[USgCh$control==1,]$Chdef,USgCh[USgCh$interd==1,]$Chdef, var.equal=F)

uu<-t.test(USgCh[USgCh$control==1,]$Chisthreat,USgCh[USgCh$offdef==1,]$Chisthreat, var.equal=F)
vv<-t.test(USgCh[USgCh$control==1,]$Chisthreat,USgCh[USgCh$secdim==1,]$Chisthreat, var.equal=F)
ww<-t.test(USgCh[USgCh$control==1,]$Chisthreat,USgCh[USgCh$nukes==1,]$Chisthreat, var.equal=F)
xx<-t.test(USgCh[USgCh$control==1,]$Chisthreat,USgCh[USgCh$interd==1,]$Chisthreat, var.equal=F)


#CREATING FIGURE 2


pdf("Fig2.pdf", height=16, width=13)

par(mfrow=c(3,1))

# Def spending

matrix1<-matrix(data=c(m$estimate[1],m$estimate[2],n$estimate[2],o$estimate[2],p$estimate[2],mm$estimate[1],mm$estimate[2],nn$estimate[2],oo$estimate[2],pp$estimate[2],a$estimate[1],a$estimate[2],b$estimate[2],c$estimate[2],d$estimate[2],aa$estimate[1],aa$estimate[2],bb$estimate[2],cc$estimate[2],dd$estimate[2]), ncol=4, byrow=F) ##create matrix for barplot

lower1<-matrix(data=c(t.test(US[US$control==1,]$defspend)$conf.int[1],t.test(US[US$offdef==1,]$defspend)$conf.int[1], t.test(US[US$secdim==1,]$defspend)$conf.int[1],t.test(US[US$nukes==1,]$defspend)$conf.int[1],t.test(US[US$interd==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$control==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$offdef==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$secdim==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$nukes==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$interd==1,]$defspend)$conf.int[1], t.test(China[China$control==1,]$defspend)$conf.int[1],t.test(China[China$offdef==1,]$defspend)$conf.int[1], t.test(China[China$secdim==1,]$defspend)$conf.int[1],t.test(China[China$nukes==1,]$defspend)$conf.int[1],t.test(China[China$interd==1,]$defspend)$conf.int[1],t.test(ChgUS[ChgUS$control==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$interd==1,]$defspend)$conf.int[1]), ncol=4, byrow=F)

upper1<-matrix(data=c(t.test(US[US$control==1,]$defspend)$conf.int[2],t.test(US[US$offdef==1,]$defspend)$conf.int[2], t.test(US[US$secdim==1,]$defspend)$conf.int[2],t.test(US[US$nukes==1,]$defspend)$conf.int[2],t.test(US[US$interd==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$control==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$offdef==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$secdim==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$nukes==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$interd==1,]$defspend)$conf.int[2], t.test(China[China$control==1,]$defspend)$conf.int[2],t.test(China[China$offdef==1,]$defspend)$conf.int[2], t.test(China[China$secdim==1,]$defspend)$conf.int[2],t.test(China[China$nukes==1,]$defspend)$conf.int[2],t.test(China[China$interd==1,]$defspend)$conf.int[2],t.test(ChgUS[ChgUS$control==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$interd==1,]$defspend)$conf.int[2]), ncol=4, byrow=F)


par(mar=c(5,4.7,4,2)+0.1)

barplot2(matrix1, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Should [your country]'s military spending increase?", names.arg=c("US (2020)", "US (2023)", "China (2020)", "China (2023)"), plot.ci=T, ci.l=lower1, ci.u=upper1, xpd=F, cex=2, cex.lab=2, cex.names=2, cex.main=2.5, main= "Support for Increasing Military Spending (7 = Increase greatly; 1 = Decrease greatly)")

segments(x0=1, x1=7, y0=m$estimate[1], y1=m$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=mm$estimate[1], y1=mm$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=a$estimate[1], y1=a$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=aa$estimate[1], y1=aa$estimate[1], lty=3, col="BLACK", lwd=1.5)

segments(x0=1, x1=26.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.15, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.6)



# Offensive/Def motivations

matrix2<-matrix(data=c(q$estimate[1],q$estimate[2],r$estimate[2],s$estimate[2],t$estimate[2],qq$estimate[1],qq$estimate[2],rr$estimate[2],ss$estimate[2],tt$estimate[2],e$estimate[1],e$estimate[2],f$estimate[2],g$estimate[2],h$estimate[2],ee$estimate[1],ee$estimate[2],ff$estimate[2],gg$estimate[2],hh$estimate[2]), ncol=4, byrow=F) ##create matrix for barplot

lower2<-matrix(data=c(t.test(US[US$control==1,]$Chisdef)$conf.int[1],t.test(US[US$offdef==1,]$Chisdef)$conf.int[1], t.test(US[US$secdim==1,]$Chisdef)$conf.int[1],t.test(US[US$nukes==1,]$Chisdef)$conf.int[1],t.test(US[US$interd==1,]$Chisdef)$conf.int[1], t.test(USgCh[USgCh$control==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$offdef==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$secdim==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$nukes==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$interd==1,]$Chdef)$conf.int[1], t.test(China[China$control==1,]$USisdef)$conf.int[1],t.test(China[China$offdef==1,]$USisdef)$conf.int[1], t.test(China[China$secdim==1,]$USisdef)$conf.int[1],t.test(China[China$nukes==1,]$USisdef)$conf.int[1],t.test(China[China$interd==1,]$USisdef)$conf.int[1],t.test(ChgUS[ChgUS$control==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$interd==1,]$USdef)$conf.int[1]), ncol=4, byrow=F)

upper2<-matrix(data=c(t.test(US[US$control==1,]$Chisdef)$conf.int[2],t.test(US[US$offdef==1,]$Chisdef)$conf.int[2], t.test(US[US$secdim==1,]$Chisdef)$conf.int[2],t.test(US[US$nukes==1,]$Chisdef)$conf.int[2],t.test(US[US$interd==1,]$Chisdef)$conf.int[2], t.test(USgCh[USgCh$control==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$offdef==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$secdim==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$nukes==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$interd==1,]$Chdef)$conf.int[2], t.test(China[China$control==1,]$USisdef)$conf.int[2],t.test(China[China$offdef==1,]$USisdef)$conf.int[2], t.test(China[China$secdim==1,]$USisdef)$conf.int[2],t.test(China[China$nukes==1,]$USisdef)$conf.int[2],t.test(China[China$interd==1,]$USisdef)$conf.int[2],t.test(ChgUS[ChgUS$control==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$interd==1,]$USdef)$conf.int[2]), ncol=4, byrow=F)

par(mar=c(5,4.7,4,2)+0.1)

barplot2(matrix2, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Are [other country]'s motivations offensive?", names.arg=c("US (2020)", "US (2023)", "China (2020)", "China (2023)"), plot.ci=T, ci.l=lower2, ci.u=upper2, xpd=F, cex=2, cex.lab=2, cex.names=2, cex.main=2.5, main= "Perception of Offensive Motivations (7 = Purely offensive; 1 = Purely defensive)")

segments(x0=1, x1=7, y0=q$estimate[1], y1=q$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=qq$estimate[1], y1=qq$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=e$estimate[1], y1=e$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=ee$estimate[1], y1=ee$estimate[1], lty=3, col="BLACK", lwd=1.5)

segments(x0=1, x1=26.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.15, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.6)

# Threat

matrix3<-matrix(data=c(u$estimate[1],u$estimate[2],v$estimate[2],w$estimate[2],x$estimate[2],uu$estimate[1],uu$estimate[2],vv$estimate[2],ww$estimate[2],xx$estimate[2],i$estimate[1],i$estimate[2],j$estimate[2],k$estimate[2],l$estimate[2],ii$estimate[1],ii$estimate[2],jj$estimate[2],kk$estimate[2],ll$estimate[2]), ncol=4, byrow=F) ##create matrix for barplot

lower3<-matrix(data=c(t.test(US[US$control==1,]$Chisthreat)$conf.int[1],t.test(US[US$offdef==1,]$Chisthreat)$conf.int[1], t.test(US[US$secdim==1,]$Chisthreat)$conf.int[1],t.test(US[US$nukes==1,]$Chisthreat)$conf.int[1],t.test(US[US$interd==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$control==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$offdef==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$secdim==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$nukes==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$interd==1,]$Chisthreat)$conf.int[1], t.test(China[China$control==1,]$USisthreat)$conf.int[1],t.test(China[China$offdef==1,]$USisthreat)$conf.int[1], t.test(China[China$secdim==1,]$USisthreat)$conf.int[1],t.test(China[China$nukes==1,]$USisthreat)$conf.int[1],t.test(China[China$interd==1,]$USisthreat)$conf.int[1],t.test(ChgUS[ChgUS$control==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$interd==1,]$USisthreat)$conf.int[1]), ncol=4, byrow=F)

upper3<-matrix(data=c(t.test(US[US$control==1,]$Chisthreat)$conf.int[2],t.test(US[US$offdef==1,]$Chisthreat)$conf.int[2], t.test(US[US$secdim==1,]$Chisthreat)$conf.int[2],t.test(US[US$nukes==1,]$Chisthreat)$conf.int[2],t.test(US[US$interd==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$control==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$offdef==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$secdim==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$nukes==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$interd==1,]$Chisthreat)$conf.int[2], t.test(China[China$control==1,]$USisthreat)$conf.int[2],t.test(China[China$offdef==1,]$USisthreat)$conf.int[2], t.test(China[China$secdim==1,]$USisthreat)$conf.int[2],t.test(China[China$nukes==1,]$USisthreat)$conf.int[2],t.test(China[China$interd==1,]$USisthreat)$conf.int[2],t.test(ChgUS[ChgUS$control==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$interd==1,]$USisthreat)$conf.int[2]), ncol=4, byrow=F)

par(mar=c(5,4.7,4,2)+0.1)

barplot2(matrix3, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Is [other country] a threat?", names.arg=c("US (2020)", "US (2023)", "China (2020)", "China (2023)"), plot.ci=T, ci.l=lower3, ci.u=upper3, xpd=F, cex=2, cex.lab=2, cex.names=2, cex.main=2.5, main= "Perception of Threat (7 = Major threat; 1 = No threat at all)")

segments(x0=1, x1=7, y0=u$estimate[1], y1=u$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=uu$estimate[1], y1=uu$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=i$estimate[1], y1=i$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=ii$estimate[1], y1=ii$estimate[1], lty=3, col="BLACK", lwd=1.5)

segments(x0=1, x1=26.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.15, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.6)



dev.off()


#CHECKING IF SURVEY EXPERIMENTS UNDERPOWERED (AS DISCUSSED IN TEXT)

# SDs for Cohen's D assessment
sd(China$defspend, na.rm=T)*.2
sd(China$USisdef, na.rm=T) *.2
sd(China$USisthreat, na.rm=T) *.2
sd(US$defspend, na.rm=T) *.2
sd(US$Chisdef, na.rm=T) *.2
sd(US$Chisthreat, na.rm=T) *.2
sd(China23$defspend, na.rm=T) *.2
sd(China23$USdef, na.rm=T) *.2
sd(China23$USisthreat, na.rm=T) *.2
sd(US23$defspend, na.rm=T) *.2
sd(US23$Chdef, na.rm=T) *.2
sd(US23$Chisthreat, na.rm=T) *.2

#Could we have detected a "small" effect?
#China2020
(a$conf.int[2]-a$conf.int[1])<sd(China$defspend, na.rm=T)*.25*2
(b$conf.int[2]-b$conf.int[1])<sd(China$defspend, na.rm=T)*.25*2
(c$conf.int[2]-c$conf.int[1])<sd(China$defspend, na.rm=T)*.25*2
(d$conf.int[2]-d$conf.int[1])<sd(China$defspend, na.rm=T)*.25*2
(e$conf.int[2]-e$conf.int[1])<sd(China$USisdef, na.rm=T)*.25*2
(f$conf.int[2]-f$conf.int[1])<sd(China$USisdef, na.rm=T)*.25*2
(g$conf.int[2]-g$conf.int[1])<sd(China$USisdef, na.rm=T)*.25*2
(h$conf.int[2]-h$conf.int[1])<sd(China$USisdef, na.rm=T)*.25*2
(i$conf.int[2]-i$conf.int[1])<sd(China$USisthreat, na.rm=T)*.25*2
(j$conf.int[2]-j$conf.int[1])<sd(China$USisthreat, na.rm=T)*.25*2
(k$conf.int[2]-k$conf.int[1])<sd(China$USisthreat, na.rm=T)*.25*2
(l$conf.int[2]-l$conf.int[1])<sd(China$USisthreat, na.rm=T)*.25*2

#US2020
(m$conf.int[2]-m$conf.int[1])<sd(US$defspend, na.rm=T)*.25*2
(n$conf.int[2]-n$conf.int[1])<sd(US$defspend, na.rm=T)*.25*2
(o$conf.int[2]-o$conf.int[1])<sd(US$defspend, na.rm=T)*.25*2
(p$conf.int[2]-p$conf.int[1])<sd(US$defspend, na.rm=T)*.25*2
(q$conf.int[2]-q$conf.int[1])<sd(US$Chisdef, na.rm=T)*.25*2
(r$conf.int[2]-r$conf.int[1])<sd(US$Chisdef, na.rm=T)*.25*2
(s$conf.int[2]-s$conf.int[1])<sd(US$Chisdef, na.rm=T)*.25*2
(t$conf.int[2]-t$conf.int[1])<sd(US$Chisdef, na.rm=T)*.25*2
(u$conf.int[2]-u$conf.int[1])<sd(US$Chisthreat, na.rm=T)*.25*2
(v$conf.int[2]-v$conf.int[1])<sd(US$Chisthreat, na.rm=T)*.25*2
(w$conf.int[2]-w$conf.int[1])<sd(US$Chisthreat, na.rm=T)*.25*2
(x$conf.int[2]-x$conf.int[1])<sd(US$Chisthreat, na.rm=T)*.25*2

#China2023
(aa$conf.int[2]-aa$conf.int[1])<sd(China23$defspend, na.rm=T)*.25*2
(bb$conf.int[2]-bb$conf.int[1])<sd(China23$defspend, na.rm=T)*.25*2
(cc$conf.int[2]-cc$conf.int[1])<sd(China23$defspend, na.rm=T)*.25*2
(dd$conf.int[2]-dd$conf.int[1])<sd(China23$defspend, na.rm=T)*.25*2
(ee$conf.int[2]-ee$conf.int[1])<sd(China23$USdef, na.rm=T)*.25*2
(ff$conf.int[2]-ff$conf.int[1])<sd(China23$USdef, na.rm=T)*.25*2
(gg$conf.int[2]-gg$conf.int[1])<sd(China23$USdef, na.rm=T)*.25*2
(hh$conf.int[2]-hh$conf.int[1])<sd(China23$USdef, na.rm=T)*.25*2
(ii$conf.int[2]-ii$conf.int[1])<sd(China23$USisthreat, na.rm=T)*.25*2
(jj$conf.int[2]-jj$conf.int[1])<sd(China23$USisthreat, na.rm=T)*.25*2
(kk$conf.int[2]-kk$conf.int[1])<sd(China23$USisthreat, na.rm=T)*.25*2
(ll$conf.int[2]-ll$conf.int[1])<sd(China23$USisthreat, na.rm=T)*.25*2

#US2023
(mm$conf.int[2]-mm$conf.int[1])<sd(US23$defspend, na.rm=T)*.2*2
(nn$conf.int[2]-nn$conf.int[1])<sd(US23$defspend, na.rm=T)*.2*2
(oo$conf.int[2]-oo$conf.int[1])<sd(US23$defspend, na.rm=T)*.2*2
(pp$conf.int[2]-pp$conf.int[1])<sd(US23$defspend, na.rm=T)*.2*2
(qq$conf.int[2]-qq$conf.int[1])<sd(US23$Chdef, na.rm=T)*.2*2
(rr$conf.int[2]-rr$conf.int[1])<sd(US23$Chdef, na.rm=T)*.2*2
(ss$conf.int[2]-ss$conf.int[1])<sd(US23$Chdef, na.rm=T)*.2*2
(tt$conf.int[2]-tt$conf.int[1])<sd(US23$Chdef, na.rm=T)*.2*2
(uu$conf.int[2]-uu$conf.int[1])<sd(US23$Chisthreat, na.rm=T)*.2*2
(vv$conf.int[2]-vv$conf.int[1])<sd(US23$Chisthreat, na.rm=T)*.2*2
(ww$conf.int[2]-ww$conf.int[1])<sd(US23$Chisthreat, na.rm=T)*.2*2
(xx$conf.int[2]-xx$conf.int[1])<sd(US23$Chisthreat, na.rm=T)*.2*2


#CREATING FIG 3


#China asymmetries for off/def
asym1<-t.test(ChgUS[ChgUS$control==1,]$USdef, ChgCh[ChgCh$control2==1,]$Chdef, var.equal=F)
asym2<-t.test(ChgUS[ChgUS$offdef==1,]$USdef, ChgCh[ChgCh$offdef2==1,]$Chdef, var.equal=F)
asym3<-t.test(ChgUS[ChgUS$secdim==1,]$USdef, ChgCh[ChgCh$secdim2==1,]$Chdef, var.equal=F)
asym4<-t.test(ChgUS[ChgUS$nukes==1,]$USdef, ChgCh[ChgCh$nukes2==1,]$Chdef, var.equal=F)
asym5<-t.test(ChgUS[ChgUS$interd==1,]$USdef, ChgCh[ChgCh$interd2==1,]$Chdef, var.equal=F)

#China asymmetries for threat
asym6<-t.test(ChgUS[ChgUS$control==1,]$USisthreat, ChgCh[ChgCh$control2==1,]$Chisthreat, var.equal=F)
asym7<-t.test(ChgUS[ChgUS$offdef==1,]$USisthreat, ChgCh[ChgCh$offdef2==1,]$Chisthreat, var.equal=F)
asym8<-t.test(ChgUS[ChgUS$secdim==1,]$USisthreat, ChgCh[ChgCh$secdim2==1,]$Chisthreat, var.equal=F)
asym9<-t.test(ChgUS[ChgUS$nukes==1,]$USisthreat, ChgCh[ChgCh$nukes2==1,]$Chisthreat, var.equal=F)
asym10<-t.test(ChgUS[ChgUS$interd==1,]$USisthreat, ChgCh[ChgCh$interd2==1,]$Chisthreat, var.equal=F)

#US asymmetries for off/def
asym11<-t.test(USgCh[USgCh$control==1,]$Chdef, USgUS[USgUS$control2==1,]$USdef, var.equal=F)
asym12<-t.test(USgCh[USgCh$offdef==1,]$Chdef, USgUS[USgUS$offdef2==1,]$USdef, var.equal=F)
asym13<-t.test(USgCh[USgCh$secdim==1,]$Chdef, USgUS[USgUS$secdim2==1,]$USdef, var.equal=F)
asym14<-t.test(USgCh[USgCh$nukes==1,]$Chdef, USgUS[USgUS$nukes2==1,]$USdef, var.equal=F)
asym15<-t.test(USgCh[USgCh$interd==1,]$Chdef, USgUS[USgUS$interd2==1,]$USdef, var.equal=F)

#US asymmetries for threat
asym16<-t.test(USgCh[USgCh$control==1,]$Chisthreat, USgUS[USgUS$control2==1,]$USisthreat, var.equal=F)
asym17<-t.test(USgCh[USgCh$offdef==1,]$Chisthreat, USgUS[USgUS$offdef2==1,]$USisthreat, var.equal=F)
asym18<-t.test(USgCh[USgCh$secdim==1,]$Chisthreat, USgUS[USgUS$secdim2==1,]$USisthreat, var.equal=F)
asym19<-t.test(USgCh[USgCh$nukes==1,]$Chisthreat, USgUS[USgUS$nukes2==1,]$USisthreat, var.equal=F)
asym20<-t.test(USgCh[USgCh$interd==1,]$Chisthreat, USgUS[USgUS$interd2==1,]$USisthreat, var.equal=F)



#CREATE FIG 3

pdf("Fig3.pdf", height=10, width=10)

par(mfrow=c(2,1))

matrix_asym<-matrix(data=c(asym11$estimate[1]-asym11$estimate[2], asym12$estimate[1]-asym12$estimate[2], asym13$estimate[1]-asym13$estimate[2], asym14$estimate[1]-asym14$estimate[2], asym15$estimate[1]-asym15$estimate[2], asym1$estimate[1]-asym1$estimate[2], asym2$estimate[1]-asym2$estimate[2], asym3$estimate[1]-asym3$estimate[2], asym4$estimate[1]-asym4$estimate[2], asym5$estimate[1]-asym5$estimate[2]), ncol=2, byrow=F)

lower_asym<-matrix(data=c(asym11$conf.int[1],asym12$conf.int[1],asym13$conf.int[1],asym14$conf.int[1],asym15$conf.int[1],asym1$conf.int[1],asym2$conf.int[1],asym3$conf.int[1],asym4$conf.int[1],asym5$conf.int[1]), ncol=2, byrow=F)

upper_asym<-matrix(data=c(asym11$conf.int[2],asym12$conf.int[2],asym13$conf.int[2],asym14$conf.int[2],asym15$conf.int[2],asym1$conf.int[2],asym2$conf.int[2],asym3$conf.int[2],asym4$conf.int[2],asym5$conf.int[2]), ncol=2, byrow=F)

barplot2(matrix_asym, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("US (2023)", "China (2023)"), plot.ci=T, ci.l=lower_asym, ci.u=upper_asym, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of offensive intentions")

segments(x0=1.2, x1=6.85, y0=asym11$estimate[1]-asym11$estimate[2], y1=asym11$estimate[1]-asym11$estimate[2], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.65, x1=13.3, y0=asym1$estimate[1]-asym1$estimate[2], y1=asym1$estimate[1]-asym1$estimate[2], lty=3, col="BLACK", lwd=1.5 )
segments(x0=1.2, x1=13.3, y0=0, y1=0, lty=1, col="BLACK")


legend(x=1.2, y=2.5, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Asymmetry in control groups" ), bty="n", cex=1.1)


matrix_asym2<-matrix(data=c(asym16$estimate[1]-asym16$estimate[2], asym17$estimate[1]-asym17$estimate[2], asym18$estimate[1]-asym18$estimate[2], asym19$estimate[1]-asym19$estimate[2], asym20$estimate[1]-asym20$estimate[2], asym6$estimate[1]-asym6$estimate[2], asym7$estimate[1]-asym7$estimate[2], asym8$estimate[1]-asym8$estimate[2], asym9$estimate[1]-asym9$estimate[2], asym10$estimate[1]-asym10$estimate[2]), ncol=2, byrow=F)

lower_asym2<-matrix(data=c(asym16$conf.int[1],asym17$conf.int[1],asym18$conf.int[1],asym19$conf.int[1],asym20$conf.int[1],asym6$conf.int[1],asym7$conf.int[1],asym8$conf.int[1],asym9$conf.int[1],asym10$conf.int[1]), ncol=2, byrow=F)

upper_asym2<-matrix(data=c(asym16$conf.int[2],asym17$conf.int[2],asym18$conf.int[2],asym19$conf.int[2],asym20$conf.int[2],asym6$conf.int[2],asym7$conf.int[2],asym8$conf.int[2],asym9$conf.int[2],asym10$conf.int[2]), ncol=2, byrow=F)

barplot2(matrix_asym2, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("US (2023)", "China (2023)"), plot.ci=T, ci.l=lower_asym2, ci.u=upper_asym2, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of threat")

segments(x0=1.2, x1=6.85, y0=asym16$estimate[1]-asym16$estimate[2], y1=asym16$estimate[1]-asym16$estimate[2], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.65, x1=13.3, y0=asym6$estimate[1]-asym6$estimate[2], y1=asym6$estimate[1]-asym6$estimate[2], lty=3, col="BLACK", lwd=1.5 )
segments(x0=1.2, x1=13.3, y0=0, y1=0, lty=1, col="BLACK")

legend(x=1.2, y=2.5, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Asymmetry in control groups" ), bty="n", cex=1.1)


dev.off()




